/**
 * Created by yangfan on 2016/10/18.
 */
/**
 * Created by yangfan on 2016/10/13.
 */
define(['config/appregister',"i18n","business/personMine/services/httpServices","jquery"],function(app,i18n,httpService){
    app.controller('othersFriendsCtrl', function($scope,$state,$stateParams,$rootScope) {
        $scope.i18n = i18n;
        $scope.everPageShow = [30,50,100];
        // 切换title
        $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.titleNavation = [
            //{title:"互粉",type:"fansEachother"},
            {title:"关注",type:"care"},
            {title:"粉丝",type:"fans"}
        ];
        $scope.isShowCurrent = [];
        $scope.isShowCurrent[0] = true;
        $scope.isMyComment = true;
        $scope.byComment = false;
        $scope.listParams = {
            pageNo:1,
            pageSize:10
        };
        if( !angular.isUndefined($stateParams.userId)){
            $scope.userId = $stateParams.userId;
            $scope.$emit('getOthersId',{userId:$scope.userId})
        }

		//获取互粉列表
		function getBothFollowList(){
			var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    userId: $scope.userId
                },
                uri:"getOtherBothFollowList"
            };
            httpService.followServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.follow = response.data.list;
                    $scope.careType = 1;
                    $scope.totalItem = response.data.Pagination.totalCount ;
                    getPaginationConf();
                })
            }).error(function(){
                console.log("访问出错");
            })
		}
		//getBothFollowList();
		//获取关注列表
		function getFollowList(){
			var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    userId: $scope.userId
                },
                uri:"getOtherFollowList"
            };
            httpService.followServices(req).success(function(response){
                $scope.$apply(function(){
                    console.log(response.data.list)
                    $scope.follow = response.data.list;
                    $scope.careType = 2;
                    $scope.totalItem = response.data.Pagination.totalCount ;
                    getPaginationConf();
                })
            }).error(function(){
                console.log("访问出错");
            })
		}
        $scope.messageConf ={
            showSuccessNotice :false,
            showErrorNotice:false,
            isShow : false ,
            content : "",
            name:'',
            toUserId:''
        };
        $scope.sendMessage = function(item,event){
            $scope.messageConf.isShow = true ;
            $scope.messageConf.name = item.name ;
            $scope.messageConf.toUserId = item.userId ;
        }

		//获取粉丝列表
		function getByFollowList(){
			var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    userId: $scope.userId
                },
                uri:"getOtherByFollowList"
            };
            httpService.followServices(req).success(function(response){
                $scope.$apply(function(){
                    $scope.follow = response.data.list;
                    $scope.careType = 3;
                    $scope.totalItem = response.data.Pagination.totalCount ;
                    getPaginationConf();
                })
            }).error(function(){
                console.log("访问出错");
            })
		}
        //待切换加载数据
        $scope.checkTitle = function(type,index){
            $scope.listParams = {
                pageNo:1,
                pageSize:10
            };
            $scope.isShowCurrent = [];
            $scope.isShowCurrent[index] = true;
            //if(index == 0){
            //    getBothFollowList();
            //}else
            if(index ==0){
                getFollowList();
            }else if(index == 1){
                getByFollowList();
            }
        };
        function init (){
            if(!angular.isUndefined($stateParams.type)){
                //if($stateParams.type == 'fansEachother'){
                //    $scope.checkTitle('fansEachother',0)
                //}
                if($stateParams.type == 'care'){
                    $scope.checkTitle('care',0)
                }
                if($stateParams.type == 'fans'){
                    $scope.checkTitle('fans',1)
                }
            }
        }
        init();
		



        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";

        function getPaginationConf(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                   // getlist()
                   // if($scope.isShowCurrent[0]){
                   //     getBothFollowList();
                   // }
                    if($scope.isShowCurrent[0]){
                        getFollowList();
                    }
                    if($scope.isShowCurrent[1]){
                        getByFollowList();
                    }
                }
            };
        };

        //取消关注
        $scope.cancelCare = function(id,eve){
        	var ele = $(eve.target);
        	ele.find('.care_message').animate({'opacity':1},function(){
        		ele.find('.care_message').animate({'opacity':0});
        		var req = {
	                type:"GET",
	                data:{
	                    followUserId:id
	                },
	                uri:"follow"
	            };
	            httpService.followServices(req).success(function(response){
                        init();
	            }).error(function(){
	                console.log("访问出错");
	            })
	        })
        };
        //添加关注
        $scope.addCare = function(id,eve){
        	var ele = $(eve.target);
        	ele.find('.care_message').animate({'opacity':1},function(){
        		ele.find('.care_message').animate({'opacity':0});
        		var req = {
	                type:"POST",
	                data:{
	                    followUserId:id
	                },
	                uri:"follow"
	            };
	            httpService.followServices(req).success(function(response){
                    init();
	            }).error(function(){
	                console.log("访问出错");
	            })
	        })
        };
        $scope.goOthersPage1 = function(item){
            if($rootScope.userLoginInfo.data.userId == item.followUserId || $rootScope.userLoginInfo.data.userId == item.userId){
                $state.go('homepage');
                return;
            }
            //去他人主页
            if(item.userId == $stateParams.userId){
                $state.go('othersMainPage',{userId:item.followUserId})
            }else if(item.followUserId == $stateParams.userId){
                $state.go('othersMainPage',{userId:item.userId})
            }

        }
    })
});
